package com.tools;

import animatefx.animation.FadeInUp;
import com.tools.constants.BaseContext;
import com.tools.constants.Constant;
import com.tools.controller.DataBaseController;
import com.tools.controller.FXMLPage;
import com.tools.controller.IndexController;
import com.tools.controller.MenuController;
import com.tools.model.DataBaseModel;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.apache.log4j.PropertyConfigurator;

import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import static com.tools.utils.SqlLiteUtil.existsConfigDB;


/**
 * 程序主入口
 *
 * @author < a href=" ">ZhangYang</ a>
 * @version v1.0.01
 * @date 2018-07-20 10:41:39
 */
public class MainApp extends Application {

  private Stage primaryStage;
  private BorderPane index;

  private IndexController indexController;
  private DataBaseController dataBaseController;
  private MenuController menuController;
  private List<Map<String, Object>> setting;
  private TrayIcon trayIcon;

  private ObservableList<DataBaseModel> dataBaseModels = FXCollections.observableArrayList();

  public MainApp() throws Exception {
    existsConfigDB();
//    String[] list = Paths.get("./template").toFile().list();
//    if(list.length==0){
//      setupRepo();
//    }
  }

  public static void main(String[] args) {
    URL logResource = Thread.currentThread().getContextClassLoader().getResource("config/log4j.properties");
    PropertyConfigurator.configure(logResource);
    launch(args);
  }

  public ObservableList<DataBaseModel> getDataBaseModels() {
    return dataBaseModels;
  }

  @Override
  public void start(Stage primaryStage) throws IOException, AWTException {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle(Constant.STRING_TITLE_TOOLS);
    Image image = new Image("image/tools.png", 100, 150, false, false);
    this.primaryStage.getIcons().add(image);
    this.primaryStage.initStyle(StageStyle.UNIFIED);
    BaseContext.controllerMap.put("this", this);
    initIndex();
    showDataBaseOverview();
//    SystemTray tray = SystemTray.getSystemTray();
//    BufferedImage image1 = ImageIO.read(MainApp.class
//                                         .getResourceAsStream("/image/tools.png"));
//    trayIcon = new TrayIcon(image1, "自动备份工具");
//    trayIcon.setToolTip("自动备份工具");
//    tray.add(trayIcon);
  }

  public void initIndex() {
    try {
      FXMLLoader fxmlLoader = new FXMLLoader();
      fxmlLoader.setLocation(Thread.currentThread().getContextClassLoader().getResource(FXMLPage.INDEX.getFxml()));
      index = fxmlLoader.load();

      Scene scene = new Scene(index);
      primaryStage.setScene(scene);
      indexController = fxmlLoader.getController();
      indexController.setPrimaryStage(primaryStage);
      indexController.setIndex(index);
      indexController.setMainApp(this);

      BaseContext.controllerMap.put("indexController", indexController);
      primaryStage.show();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public void showDataBaseOverview() {
    try {
      FXMLLoader loader = new FXMLLoader();
      loader.setLocation(Thread.currentThread().getContextClassLoader().getResource(FXMLPage.MENU.getFxml()));
      AnchorPane menuOverview = loader.load();

      menuController = loader.getController();
      menuController.setPrimaryStage(primaryStage);
      menuController.setIndex(index);

      index.setCenter(menuOverview);

      BaseContext.controllerMap.put("menuController", menuController);
      new FadeInUp(menuOverview).play();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public Stage getPrimaryStage() {
    return primaryStage;
  }

  public IndexController getIndexController() {
    return indexController;
  }

  public void setIndexController(IndexController indexController) {
    this.indexController = indexController;
  }

  public DataBaseController getDataBaseController() {
    return dataBaseController;
  }

  public void setDataBaseController(DataBaseController dataBaseController) {
    this.dataBaseController = dataBaseController;
  }

  public List<Map<String, Object>> getSetting() {
    return setting;
  }

  public void setSetting(List<Map<String, Object>> setting) {
    this.setting = setting;
  }
}
